#include "oled_display.h"
#include "bsp_oled.h"
#include "bsp_delay.h"
#include "bsp_key.h"
#include "bsp_micros.h"
#include "bsp_soft_i2c.h"
#include "stdio.h"
#include "bsp_adc.h"
#include "bsp_flash.h"
#include "power_control.h"
#include "usart.h"
volatile uint8_t now_menu_index;  //当前的菜单栏目录
volatile uint8_t last_menu_index=100;        //上次的菜单栏目录
extern uint32_t id_reading;
//***************bsp_key.c中的变量***************//

char buff_id[8];    //ID的字符串缓存
char buffC0[4], buffC1[4];
char buffC2[4];
char buffVO[8]; //电压值缓存
char buffV[4];     //电压值缓存


/**
 * @brief 显示屏幕初始化
 * @param  no
 */
void display_init(void)
{
  OLED_Init();
  OLED_CLS();
}

/**
 * @brief 菜单栏显示循环
 * @param  no 
 */
void display_loop(void) {
  static uint16_t cnt = 0;
  if (now_menu_index != last_menu_index) {
    cnt = 0;  //清楚返回主菜单栏标志位
    key_index[now_menu_index].operate();
    last_menu_index = now_menu_index;
  }
  else {
    if (cnt++ > 300) {
      now_menu_index = home;
      cnt = 0;
    }
  }
  if (now_menu_index == home) {
    refresh_main_data();
		
  }
  else return;
}
/**
 * @brief 窗口显示切换
 * @param page 页数
 * @param rows 行数
 */
void oled_show_windows(uint8_t page, uint8_t rows) {
  OLED_CLS();   //清屏
  switch (rows) {
  case f_f:
    OLED_ShowStr(0, 0, (uint8_t*)"->", 2);
    break;
  case f_s:
    OLED_ShowStr(0, 2, (uint8_t*)"->", 2);
    break;
  case f_t:
    OLED_ShowStr(0, 4, (uint8_t*)"->", 2);
    break;
  case f_b:
    OLED_ShowStr(0, 6, (uint8_t*)"->", 2);
  default:
    break;
  }
  if (page == 1) {

    OLED_ShowStr(16, 0, (uint8_t*)"ID", 2);//显示字符串
    OLED_ShowCN_STR(32, 0, 0, 2);  //设置
    sprintf(buff_id, ":0x%x", (uint32_t)id_reading);
    OLED_ShowStr(64, 0, (uint8_t*)buff_id, 2);
    OLED_ShowCN_STR(16, 2, 8, 4);  //模式切换
    OLED_ShowCN_STR(16, 4, 6, 2);  //语言
    OLED_ShowCN_STR(16, 6, 12, 2); //返回
  }
  else if (page == 2) {
    OLED_ShowStr(0, 0, (uint8_t*)"Target_ID:", 2);//显示字符串
    OLED_ShowStr(0, 2, (uint8_t*)"Target_FU:", 2);//显示字符串
    OLED_ShowStr(0, 4, (uint8_t*)"Laguages:", 2);//显示字符串
    OLED_ShowStr(0, 6, (uint8_t*)"return <-", 2);//显示字符串
  }
}

/**
 * @brief c主菜单显示的数据
 * @param page 
 * @param rows 
 */
void home_show(void) {
  OLED_CLS();   //清屏
  OLED_ShowCN_STR(0, 0, 16, 2); //电流  

  sprintf(buffC0, "%2.2f", sample.in_c);
  OLED_ShowStr(36, 0, buffC0, 2);

  sprintf(buffC1, "%2.2f", sample.out_c);
  OLED_ShowStr(80, 0, buffC1, 2);

  OLED_ShowStr(0, 2, "VIN:", 2);
  sprintf(buffC2, "%3.1f", sample.in_v);
  OLED_ShowStr(32, 2, buffC2, 2);
	
  OLED_ShowStr(70, 2, "VC:", 2);
	sprintf(buffVO, "%3.1f", sample.cap_v);
  OLED_ShowStr(86, 2, buffVO, 2);

  OLED_ShowCN_STR(0, 4, 24, 2); //  OLED_ShowCN_STR(32, 4, 18, 2); //电容功率
  sprintf(buffV, "P=%3.1f", sample.cap_p);
  OLED_ShowStr(36, 4, buffV, 2);

  OLED_ShowStr(0, 6, "<---", 2);    //切换下一页
  OLED_ShowCN_STR(42, 6, 10, 2);
  OLED_ShowStr(85, 6, "--->", 2);   //切换下一页
}

void f_f_show(void) {
  oled_show_windows(1, 1);
}
void f_s_show(void) {
  oled_show_windows(1, 2);
}
void f_t_show(void) {
  oled_show_windows(1, 3);
}
void f_b_show(void) {
  oled_show_windows(1, 4);
}
void f_f_f_show(void) {
  oled_show_windows(2, 1);
}
void f_f_s_show(void) {
  oled_show_windows(2, 2);
}
void f_f_t_show(void) {
  oled_show_windows(2, 3);
}
void f_f_b_show(void) {
  oled_show_windows(2, 4);
}

/**
 * @brief 更新主要的数据
 * @param  no
 */
void refresh_main_data(void) {
  sprintf(buffC0, "%2.2f", sample.in_c);
  OLED_ShowStr(36, 0, buffC0, 2);
  sprintf(buffC1, "%2.2f", sample.out_c);
  OLED_ShowStr(80, 0, buffC1, 2);
  sprintf(buffC2, "%3.1f", sample.in_v);
  OLED_ShowStr(32, 2, buffC2, 2);
  sprintf(buffVO, "%3.1f", sample.cap_v);
  OLED_ShowStr(86, 2, buffVO, 2);
  sprintf(buffV,"P=%3.1f",sample.cap_p);
  OLED_ShowStr(36, 4, buffV, 2);
}

			 
